﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;

namespace Spencen.Mobile.UI.Transforms
{
    public class ScaleTransform : Transform
    {
        public ScaleTransform() : this( 1.0f, 1.0f ) { }

        public ScaleTransform( float scaleX, float scaleY )
        {
            ScaleX = scaleX;
            ScaleY = scaleY;
        }

        public float ScaleX { get; set; }
        public float ScaleY { get; set; }

        public override Point[] TransformedPoints( Point[] points, Point center )
        {
            return TransformedPoints( points, center, ScaleX, ScaleY );
        }

        public override Point[] TransformedPointsReverse( Point[] transformedPoints, Point center )
        {
            return TransformedPoints( transformedPoints, center, 1 / ScaleX, 1 / ScaleY );
        }

        private static Point[] TransformedPoints( Point[] points, Point center, float scaleX, float scaleY )
        {
            var scaledPoints = new Point[ points.Length ];
            for ( int index = 0; index < points.Length; index++ )
            {
                var point = points[ index ];
                var offsetFromCenter = new Point( point.X - center.X, point.Y - center.Y );
                scaledPoints[ index ].X = (int)( offsetFromCenter.X * scaleX + center.X );
                scaledPoints[ index ].Y = (int)( offsetFromCenter.Y * scaleY + center.Y );
            }
            return scaledPoints;
        }
    }
}
